﻿using Learun.Application.Organization;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using static Learun.Application.TwoDevelopment.ZZDT_EC.ProjectSugar;
using static Learun.Util.SqlSugar.SqlSugarHelper;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-15 09:55
    /// 描 述：报表目录表
    /// </summary>
    public class ec_report_catalogueBLL : ec_report_catalogueIBLL
    {
        private ec_report_catalogueService ec_report_catalogueService = new ec_report_catalogueService();

        #region 获取数据

        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_report_catalogueEntity> GetList(string queryJson, Pagination pagination = null)
        {
            try
            {
                var queryParam = queryJson.ToJObject();
                var ProjectId = queryParam["ProjectId"]?.ToString();
                var ReportCatalogueName = queryParam["ReportCatalogueName"]?.ToString();
                var CurReportCatalogueID = queryParam["CurReportCatalogueID"]?.ToString();

                var tableName = TableName<ec_report_catalogueEntity>(ProjectId);
                var query = Db.Queryable<ec_report_catalogueEntity>().AS(tableName)
                    .WhereIF(!string.IsNullOrEmpty(ReportCatalogueName), x => x.ReportCatalogueName.Contains(ReportCatalogueName))
                    .WhereIF(!string.IsNullOrEmpty(CurReportCatalogueID), x => !x.FullPath.Contains(CurReportCatalogueID))
                    .OrderBy(x => x.OrderID);
                List<ec_report_catalogueEntity> list;
                if (pagination == null)
                {
                    list = query.ToList();
                }
                else
                {
                    list = query.ToPageList(pagination.page, pagination.rows);
                }
                var users = Db.Queryable<UserEntity>().ToList();
                list?.ForEach(x =>
                {
                    x.CreateUserName = users.FirstOrDefault(y => y.F_UserId == x.CreateUserID)?.F_RealName;
                });
                return list;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_report_catalogueEntity GetEntity(string ProjectId, string keyValue)
        {
            try
            {
                return ec_report_catalogueService.GetEntity(ProjectId, keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 获取报表目录树形数据
        /// </summary>
        /// <param name="queryJson"></param>
        /// <returns></returns>
        public List<TreeModel> GetTreeData(string queryJson)
        {
            try
            {
                var list = GetList(queryJson);
                List<TreeModel> treeList = new List<TreeModel>();
                foreach (var item in list)
                {
                    TreeModel node = new TreeModel();
                    node.id = item.ReportCatalogueID;
                    node.text = item.ReportCatalogueName;
                    node.value = item.ReportCatalogueID;
                    node.showcheck = false;
                    node.checkstate = 0;
                    node.isexpand = false;
                    node.parentId = item.UpReportCatalogueID;
                    node.NodeExtData = item;
                    treeList.Add(node);
                }
                return treeList.ToTree();
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string ProjectId, string keyValue)
        {
            try
            {
                ec_report_catalogueService.DeleteEntity(ProjectId, keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string ProjectId, string keyValue, ec_report_catalogueEntity entity)
        {
            try
            {
                ec_report_catalogueService.SaveEntity(ProjectId, keyValue, entity);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

    }
}
